<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div v-for="(item,i) in roleList" :key="item.roleId" class="role-item" :class="{active:i===activeIndex}" @click="changeRole(i)">
        <div class="role-info">
          <svg-icon :icon-class="activeIndex===i?'user-active':'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" class="icon" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$router.push(`/role/add/${item.roleId}`)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="delRole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/role/add')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree" />
        <div class="tree-wrapper">
          <div v-for="item in treelist" :key="item.id" class="tree-item">
            <div class="tree-title"> {{ item.title }} </div>
            <el-tree
              ref="treeRef"
              :data="item.children"
              node-key="id"
              :props="{label:'title'}"
              show-checkbox
              check-strictly
              :default-expand-all="true"
            >
            <!-- <el-tree-column prop="title" /> -->
            </el-tree>
          </div>
        </div>
        <el-tab-pane :label="`成员(${roleUserList.length})`" name="member">
          <div class="user-wrapper">
            <el-table
              :data="roleUserList"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane></el-tabs></div>
  </div></template>

<script>
import { delRoleUserAPI, getAllPermissionAPI, getRoleDetailAPI, getRoleListAPI, getRoleUserAPI } from '@/api/role'

export default {
  name: 'Role',
  data() {
    return {
      roleList: [],
      activeIndex: 0,
      treelist: [],
      perms: [],
      activeName: 'tree',
      roleUserList: [],
      total: 0
    }
  },
  created() {
    this.getRoleList()
    this.getTreeList()
  },
  async mounted() {
    await this.getRoleList()
    await this.getTreeList()
    this.getRoleDetail(this.roleList[0].roleId)
    this.getRoleUser(this.roleList[0].roleId)
  },
  methods: {
    delRole(roleId) {
      this.$confirm('是否确认删除当前角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoleUserAPI(roleId)
        this.getRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    async getRoleUser(roleId) {
      const { total, rows } = await getRoleUserAPI(roleId)

      this.roleUserList = rows
      this.total = total
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },
    async getRoleDetail(roleId) {
      const res = await getRoleDetailAPI(roleId)
      this.perms = res.perms

      const list = this.$refs.treeRef
      // 禁用了，勾选的效果不是那么明显
      // setCheckedKeys 恢复之前勾选的效果
      list.forEach((tree, index) => tree.setCheckedKeys(this.perms[index]))
    },
    changeRole(id) {
      this.activeIndex = id
      const { roleId } = this.roleList[id]
      this.getRoleDetail(roleId)
      this.getRoleUser(roleId)
    },
    addDisabled(treeList) {
      treeList.forEach(item => {
        item.disabled = true
        // 递归处理
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
    },
    async getTreeList() {
      this.treelist = await getAllPermissionAPI()
      this.addDisabled(this.treelist)
    },
    async getRoleList() {
      this.roleList = await getRoleListAPI()
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
      color:red;
      background-color: red;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
